﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Models.CopyRight;
using X.PagedList;
using STF.Blog.Common;
using STF.Blog.WebApp.Areas.ForumManage.Filters;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class CopyRightController : Controller
    {

        private ICopyrightService _icr;
        private IWebHostEnvironment _iwe;
        public CopyRightController(ICopyrightService icr, IWebHostEnvironment iwe)
        {
            _icr = icr;
            _iwe = iwe;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search,int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _icr.GetDateByName(search);

            var CopyrList = data.Select(cp => new CopyRightListViewModel 
            {
                Id = cp.Id,
                Title = cp.Title,
                Details = cp.Details,
                Images = cp.Images,
                Logo = cp.Logo,
                Address = cp.Address,
                Tel1 = cp.Tel1,
                Tel2 = cp.Tel2,
                QQ1 = cp.QQ1,
                QQ2 = cp.QQ2,
                Email = cp.Email,
                CreateTime = cp.CreateTime,
                UpdateTime = cp.UpdateTime
            });

            ViewBag.Search = search;
            var pages = CopyrList.ToPagedList(page,PageConfig.PageSize);

            return View(pages);
        }

        public async Task<IActionResult> Delete(int id) 
        {
            await _icr.PutTrashAsync(id);
            return Redirect("~/ForumManage/CopyRight/Index");
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            return View(new CopyRightAddViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> Add(CopyRightAddViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                var imageName = await UploadFiles(model.Images, @"\ImageHead\CopyRight");
                if (string.IsNullOrEmpty(imageName))
                {
                    imageName = "123.jpg";
                }

                var res = await _icr.AddUsersAsync(model.Title,model.Details,imageName,model.Logo,model.Address
                                                    ,model.Tel1,model.Tel2,model.QQ1,model.QQ2,model.Email);

                if(res > 0)
                    return Redirect("~/ForumManage/CopyRight/Index");
            }
            return View(model);
        }


        private async Task<string> UploadFiles(IFormFile image, string url)
        {
            string imageName = "";
            if (image != null && image.FileName != null)
            {
                imageName = DateTime.Now.ToString("yyyyMMddHHmmss") +
                    image.FileName.Substring(image.FileName.LastIndexOf("."));

                string imagePath = _iwe.WebRootPath + url;  // 上传到目标目录(文件夹)
                string path = Path.Combine(imagePath, imageName);   // 把路径转换为绝对路径
                image.CopyTo(new FileStream(path, FileMode.Create));
            }
            return imageName;
        }
    }
}
